﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Channels;

namespace suanpan.API.Components.Channels.Core
{
    class RestChessChannelListener : ChannelListenerBase<IReplyChannel>
    {

        #region Properties

        protected IChannelListener<IReplyChannel> InnerChannelListener { get; set; }

        #endregion

        #region CTor

        public RestChessChannelListener(BindingContext context)
        {
            this.InnerChannelListener = context.BuildInnerChannelListener<IReplyChannel>();
            if (this.InnerChannelListener == null)
                throw new InvalidOperationException("RestChessChannelListener requires an inner IChannelListener");
        }

        #endregion

        #region ChannelListenerBase Overrides

        protected override IReplyChannel OnAcceptChannel(TimeSpan timeout)
        {
            IReplyChannel innerChannel = this.InnerChannelListener.AcceptChannel(timeout);
            if (innerChannel == null)
            {
                return null;
            }
            else
            {
                return new RestChessChannel(this, (IReplyChannel)innerChannel);
            }
        }

        protected override IAsyncResult OnBeginAcceptChannel(TimeSpan timeout, AsyncCallback callback, object state)
        {
            return this.InnerChannelListener.BeginAcceptChannel(timeout, callback, state);
        }

        protected override IReplyChannel OnEndAcceptChannel(IAsyncResult result)
        {
            IReplyChannel innerChannel = this.InnerChannelListener.EndAcceptChannel(result);
            return new RestChessChannel(this, (IReplyChannel)innerChannel);
        }

        protected override IAsyncResult OnBeginWaitForChannel(TimeSpan timeout, AsyncCallback callback, object state)
        {
            return this.InnerChannelListener.BeginWaitForChannel(timeout, callback, state);
        }

        protected override bool OnEndWaitForChannel(IAsyncResult result)
        {
            return this.InnerChannelListener.EndWaitForChannel(result);
        }

        protected override bool OnWaitForChannel(TimeSpan timeout)
        {
            return this.InnerChannelListener.WaitForChannel(timeout);
        }

        public override Uri Uri
        {
            get { return this.InnerChannelListener.Uri; }
        }

        public override T GetProperty<T>()
        {
            T baseProperty = base.GetProperty<T>();
            if (baseProperty != null)
            {
                return baseProperty;
            }

            return this.InnerChannelListener.GetProperty<T>();
        }

        protected override void OnAbort()
        {
            this.InnerChannelListener.Abort();
        }

        protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)
        {
            return this.InnerChannelListener.BeginClose(timeout, callback, state);
        }

        protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
        {
            return this.InnerChannelListener.BeginOpen(timeout, callback, state);
        }

        protected override void OnClose(TimeSpan timeout)
        {
            this.InnerChannelListener.Close(timeout);
        }

        protected override void OnEndClose(IAsyncResult result)
        {
            this.InnerChannelListener.EndClose(result);
        }

        protected override void OnEndOpen(IAsyncResult result)
        {
            this.InnerChannelListener.EndOpen(result);
        }

        protected override void OnOpen(TimeSpan timeout)
        {
            this.InnerChannelListener.Open(timeout);
        }

        #endregion
    }
}
